package com.mongoosehome.web.base;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import org.json.JSONObject;

@SuppressWarnings("serial")
public class BaseActon extends ActionSupport implements SessionAware,
        ServletRequestAware, ServletResponseAware {

    static Logger logger = Logger.getLogger(BaseActon.class);
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected SessionMap session;
    protected String id;
    protected String tabid;

    @SuppressWarnings("unchecked")
    @Override
    public void setSession(Map map) {
        this.session = (SessionMap) map;

    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;

    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.response = response;

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTabid() {
        return tabid;
    }

    public void setTabid(String tabid) {
        this.tabid = tabid;
    }
    
    public void responseWriteJson(String jsonString) {
        try {
            response.setContentType("text/json; charset=utf-8");
            response.getWriter().write(jsonString);
        } catch (IOException e) {
            try {
                response.getWriter().close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            try {
                response.getWriter().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
